﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Tests.TestObjects;

namespace SharedGenomics.Workbench.Tests.Annotations
{
    [TestClass]
    public class AnnotationRequestPipelineTests
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ShouldNotAddStageIfNull()
        {
            var pipeline = new AnnotationRequestPipeline();
            pipeline.AddRequestStage(null);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldNotAddStageIfFixed()
        {
            var pipeline = new AnnotationRequestPipeline();
            pipeline.Fix();
            pipeline.AddRequestStage(new TestAnnotationRequestStage());
        }

        [TestMethod]
        public void ShouldExecutePipeline()
        {
            var stage1 = new TestAnnotationRequestStage();
            var stage2 = new TestAnnotationRequestStage();

            var pipeline = new AnnotationRequestPipeline();
            pipeline.AddRequestStage(stage1);
            pipeline.AddRequestStage(stage2);
            pipeline.Fix();

            var requestInfo = new AnnotationRequestInfo { Key = AnnotationKey.GoogleSearch, RootValue = "rs001" };
            pipeline.Execute(requestInfo);

            Assert.IsTrue(stage1.WasCalled);
            Assert.IsTrue(stage2.WasCalled);
        }

        [TestMethod]
        public void ShouldExecuteEmptyPipeline()
        {
            //no exception should be thrown if pipeline is empty
            var pipeline = new AnnotationRequestPipeline();
            pipeline.Fix();
            pipeline.Execute(new AnnotationRequestInfo { Key = AnnotationKey.GoogleSearch, RootValue = "rs001" });

            //test passes implicitly
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldNotExecuteUnfixedPipeline()
        {
            var pipeline = new AnnotationRequestPipeline();

            //should not be allowed to execute pipeline without it being fixed
            pipeline.Execute(new AnnotationRequestInfo { Key = AnnotationKey.GoogleSearch, RootValue = "rs001" });
        }
    }
}
